package LawsonTransformJava.pgm;


import java.sql.*;
import java.util.Date;
import java.io.*;
import java.lang.reflect.InvocationTargetException;

import LawsonTransformJava.dao.DAOEmAchDepst;
import LawsonTransformJava.dao.DAOFactoryDB;
import LawsonTransformJava.dao.DAObject;
import LawsonTransformJava.data.TblEmAchDepst;
import LawsonTransformJava.main.ClassMain;
import LawsonTransformJava.xref.CrossRef;
import LawsonTransformJava.xref.XrefEmployee;
import LawsonTransformJava.xref.XrefJobCode;

public class PR512 extends DAObject implements 
 DAOEmAchDepst, Runnable {

	private static XrefEmployee xEmployees = null;

	public PR512(File output, File errOutput, File errReport) throws SQLException {
		// initialization 
		try {
			this.output = new FileWriter(this.outputFile = output);
			this.errOutput = new FileWriter(this.errOutputFile = errOutput);
			this.errReport = new Report(errReport, new Date());

			init();
		} catch (IOException e) {
			// Could no write properties file
			System.out.println("IOException: " + e);
		}
	}

	private void init() throws IOException, SQLException {
		// Set the data variables
		data = DAOFactoryDB.getDAODB(true);
		xEmployees = new XrefEmployee();

		errReport.setPgmName(this.getClass().getSimpleName());

		sqlData = "SELECT * FROM " + data.getSchema() + TABLE_NAME;
		stmtData = data.getDataStatement();	    
		countStmtData = data.getCountStatement();
	}

public TblEmAchDepst findEmachdepst() {
		try {
			TblEmAchDepst ead = new TblEmAchDepst();
			sqlData = "SELECT * FROM " + data.getSchema() + TABLE_NAME + (whereClause==null?"":whereClause);
			rslt = data.getDataStatement().executeQuery(sqlData);
			rsmd = rslt.getMetaData();
			if (rslt.next())
				hasMoreRecs = true;
			else
				hasMoreRecs = false;
			ead = (TblEmAchDepst) createObject(rslt, ead);
			return ead;	

		} catch (SQLException e) {
		    // Could not connect to the database
			System.out.println("SQLException: " + e);
		} catch (InvocationTargetException ite) {
			System.out.println("InvocationException: " + ite);
		} catch (IllegalAccessException iae) {
			System.out.println("IllegalAccessException: " + iae);
		}
	  return null;
}

public TblEmAchDepst findEmachdepst(String employeeNumber) {
	try {		
		TblEmAchDepst ead = new TblEmAchDepst();
		sqlData = "SELECT * FROM " + data.getSchema() + TABLE_NAME + " WHERE " + TABLE_NAME + "='" + employeeNumber + "'";
		rslt = data.getDataStatement().executeQuery(sqlData);
		rsmd = rslt.getMetaData();
		rslt.next();
		//@ TODO Add the creation of an Employee object here
		ead = (TblEmAchDepst) createObject(rslt, ead);
		return ead;
		//createFields(ead);
	} catch (SQLException e) {
	    // Could not connect to the database
		System.out.println("SQLException: " + e);
	} catch (InvocationTargetException ite) {
		System.out.println("InvocationException: " + ite);
	} catch (IllegalAccessException iae) {
		System.out.println("IllegalAccessException: " + iae);
	}
	return new TblEmAchDepst();
}

public TblEmAchDepst nextEmachdepst() {
		try {
			TblEmAchDepst ead = new TblEmAchDepst();
			if (rslt.next()) {
				hasMoreRecs = true;
				ead = (TblEmAchDepst) createObject(rslt, ead);
			}
			else
				hasMoreRecs = false;
			return ead;
		} catch (SQLException e) {
		    // Could not connect to the database
			System.out.println("SQLException: " + e);
		} catch (InvocationTargetException ite) {
			System.out.println("InvocationException: " + ite);
		} catch (IllegalAccessException iae) {
			System.out.println("IllegalAccessException: " + iae);
		}
	  return null;
}

	public boolean hasMore () {
	  return hasMoreRecs;
	}

public int countEmAchDepst() {
	try {
		countRecs = data.getCountStatement().executeQuery("SELECT COUNT(*) FROM " + data.getSchema() + TABLE_NAME + (whereClause==null?"":whereClause));
		countRecs.next();
		return recCount=countRecs.getInt(1);
		} catch (SQLException e) {
		    // Could not connect to the database
			System.out.println("SQLException: " + e);
		}
		return 0;
}

public void setWhereClause(String where) {
	  this.whereClause = where;
}

public void run() {
	  TblEmAchDepst ead = null;
	  
	  try {
		  //Update current date/time
		  System.out.println(errReport.getName() + " Start " + Report.getDateFormat().format(new Date()));

		  ead = findEmachdepst();
		  while (hasMore()) {
			  writePR512Layout(ead);
			  ead = nextEmachdepst();
		  }
		  //Update current date/time
		  System.out.println(errReport.getName() + " Finish " + Report.getDateFormat().format(new Date()));

		  //Write Record Counts
		  errReport.writeRecordCounts(this.countEmAchDepst());

		  //Reclaim Resources
		  fileCleanup();

	} catch (IOException e) {
		// Could not write properties file
		// Could not create output file
		System.out.println("IOException: " + e);
	}

}

	//Output the PR512 format
	public void writePR512Layout(TblEmAchDepst ead) throws IOException {

		String writeValue = null;
		//Used to verify the default flag for ACH
		String defaultFlag = ead.getDefault_flag();

		writeValue =
//				"\""+"C"+"\"" + "," +			//FC - Change
				"\""+"A"+"\"" + "," +			//FC - Add
				ClassMain.COMPANY + "," +
  				xEmployees.xlate(Integer.toString(ead.getEmployee())) + "," + 
				ead.getAch_dist_order() + "," +
				ead.getOccurances() + "," +
				ead.getRemain_occur() + "," +
				ead.getEbank_id() + "," +
				ead.getEbnk_acct_nbr() + "," +
				ead.getAccount_type() + "," +
				ead.getBank_roll_no() + "," +
				ead.getAch_prenotify() + "," +
				ead.getNet_percent() + "," +
				ead.getDeposit_amt() + "," +
				"\"" + defaultFlag + "\"" + "," +
				ead.getDescription() + "," +
				ead.getCheck_desc() + "," +
				ead.getBeg_date() + "," +
				ead.getEnd_date() + "," +
//				ead.getDed_cycle_01() + "," +			//Ded Cycle 1
//				ead.getDed_cycle_02() + "," +			//Ded Cycle 2
//				ead.getDed_cycle_03() + "," +			//Ded Cycle 3
//				ead.getDed_cycle_04() + "," +			//Ded Cycle 4
//				ead.getDed_cycle_05() + "," +			//Ded Cycle 5
//				ead.getDed_cycle_06() + "," +			//Ded Cycle 6
//				ead.getDed_cycle_07() + "," +			//Ded Cycle 7
//				ead.getDed_cycle_08() + "," +			//Ded Cycle 8
//				ead.getDed_cycle_09() + "," +			//Ded Cycle 9
				//Force client deduction Cycles 1,2,3 & 6
				"\"X\"" + "," +							//Ded Cycle 1
				"\"X\"" + "," +							//Ded Cycle 2
				"\"X\"" + "," +							//Ded Cycle 3
				"\"\"" + "," +							//Ded Cycle 4
				"\"\"" + "," +							//Ded Cycle 5
				"\"X\"" + "," +							//Ded Cycle 6
				"\"\"" + "," +							//Ded Cycle 7
				"\"\"" + "," +							//Ded Cycle 8
				"\"\"" + "," +							//Ded Cycle 9
				ead.getCa_inst_nbr() + "," +
				ead.getCa_transit_nbr() + "," +
				ead.getPayable_to() + "," +
				// TODO Default Print Receipt to Default Flag - Y only allowed for Default Y
				(defaultFlag.equals("Y")?2:"")		//Print Receipt - 2 = Yes
//				"\""+"N"+"\"" 				//Print Receipt - Default to N
			;
		
		if (CrossRef.isError()) {
			//Increment Record in Error
			errReport.addErrorCnt();
			//Write to Error File
			this.errOutput.write(writeValue + "\n");
			this.errOutput.flush();
			CrossRef.resetError();
		}
		else {
			//Increment Record Written
			errReport.addWrittenCnt();
			//Write to Load File
			this.output.write(writeValue + "\n");
			this.output.flush();
		}
	}

	public void fileCleanup() throws IOException {
		errReport.flush();
		errReport.close();
		output.flush();
		output.close();
		errOutput.flush();
		errOutput.close();
	}
}